import * as THREE from "three";
// 通用射线拾取函数，对group和mesh的选择都是有效的
import config from "./config";
import { renderer } from "./renderer.js";

function raychoose(event, chooseObjArr, camera) {
  const rect = renderer.domElement.getBoundingClientRect();
  const Sx = event.clientX - rect.left; //鼠标单击位置横坐标
  const Sy = event.clientY - rect.top; //鼠标单击位置纵坐标
  //   console.log("鼠标单击位置", Sx, Sy);
  //屏幕坐标转WebGL标准设备坐标
  const x = (Sx / config.cWidth) * 2 - 1; //WebGL标准设备横坐标
  const y = -(Sy / config.cHeight) * 2 + 1; //WebGL标准设备纵坐标
  //   console.log(x, y);

  //创建一个射线投射器`Raycaster`
  const raycaster = new THREE.Raycaster();
  //通过鼠标单击位置标准设备坐标和相机参数计算射线投射器`Raycaster`的射线属性.ray
  raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
  //   console.log("raycaster", raycaster);

  //返回.intersectObjects()参数中射线选中的网格模型对象
  // 未选中对象返回空数组[],选中一个数组1个元素，选中两个数组两个元素
  const intersects = raycaster.intersectObjects(chooseObjArr, true);

  // console.log('intersects,',intersects);
  //   console.log("射线器返回的对象", intersects, chooseObjArr);
  // intersects.length大于0说明，说明选中了模型
  let choose = null;
  if (intersects.length > 0) {
    choose = intersects[0].object;
  }
  return choose;
}

// function raychoose(event, chooseObjArr, camera) {
//   const rect = renderer.domElement.getBoundingClientRect();
//   const x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
//   const y = -((event.clientY - rect.top) / rect.height) * 2 + 1;

//   const raycaster = new THREE.Raycaster();
//   raycaster.setFromCamera({ x, y }, camera);

//   camera.updateMatrixWorld();

//   // 第二个参数 true → 递归检测子对象
//   let intersects = raycaster.intersectObjects(chooseObjArr, true);

//   // 过滤掉 Sprite / Tag / 透明 plane
//   intersects = intersects.filter(i => i.object.type === "Mesh");

//   console.log("射线检测结果:", intersects);
//   return intersects.length > 0 ? intersects[0].object : null;
// }

export { raychoose };
